package main

import (
	"fmt"
	"github.com/influxdata/influxdb/client/v2"
	"net/url"
	"time"
)

func main() {
	opt, err := url.Parse("influx://moyrn:moyrn@118.89.29.19:8086")
	if err != nil {
		panic(err)
	}

	password, _ := opt.User.Password()

	// 连接
	httpClient, err := client.NewHTTPClient(client.HTTPConfig{
		Addr:     fmt.Sprintf("http://%s", opt.Host),
		Username: opt.User.Username(),
		Password: password,
		Timeout:  time.Second * 5,
	})

	if err != nil {
		panic(err)
	}

	// 建表
	batchPoints, err := client.NewBatchPoints(client.BatchPointsConfig{
		Precision: "ms",
		Database:  "demo",
	})

	if err != nil {
		panic(err)
	}

	point, err := client.NewPoint(
		"demo_table",
		map[string]string{"tag_1": "tag_value_1", "tag_2": "tag_value_2", "tag_3": "tag_value_3", "tag_4": "tag_value_4", "tag_5": "tag_value_5"},
		map[string]interface{}{"field_1": "field_value_1", "field_2": "field_value_2", "field_3": "field_value_3", "field_4": "field_value_4", "field_5": "field_value_5"},
	)

	if err != nil {
		panic(err)
	}

	batchPoints.AddPoint(point)

	if err := httpClient.Write(batchPoints); err != nil {
		panic(err)
	}

}
